<template>
  <div>
    <el-form ref="form" label-width="90px">
      <el-form-item label="SPU名称"> {{ spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格（元）">
        <el-input
          placeholder="价格"
          v-model="skuInfo.price"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(kg)">
        <el-input placeholder="重量" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          placeholder="规格描述"
          type="textarea"
          rows="4"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <!-- 两个水平的下拉框用到行内表单 -->
        <el-form :inline="true" ref="form1" label-width="80px">
          <el-form-item
            :label="item.attrName"
            v-for="(item, index) in infoList"
            :key="index"
          >
            <!-- 将数据收集在平台属性上，将来直接整理提交服务器 -->
            <el-select placeholder="请选择" v-model="item.idInfo">
              <el-option
                :label="item1.valueName"
                :value="`${item.id}:${item1.id}`"
                v-for="(item1, index1) in item.attrValueList"
                :key="index1"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" ref="form1" label-width="80px">
          <el-form-item
            :label="saleList.saleAttrName"
            v-for="(saleList, index) in saleList"
            :key="index"
          >
            <el-select placeholder="请选择" v-model="saleList.idInfo">
              <el-option
                :label="valueList.saleAttrValueName"
                :value="`${saleList.id}:${valueList.id}`"
                v-for="(valueList, index1) in saleList.spuSaleAttrValueList"
                :key="index1"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="imageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" label="label" width="80">
          </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                v-if="row.isDefault == 0"
                @click="setDefault(row)"
                >设为默认</el-button
              >
              <el-button type="success" size="mini" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      spuName: "",
      imageList: [], // 图片列表
      saleList: [], // 销售属性列表
      infoList: [], // 平台属性列表
      // 需要收集的sku信息
      skuInfo: {
        category3Id: null,
        price: null,
        spuId: null,
        tmId: null,
        skuName: "", // 名字
        skuDefaultImg: "", // 默认图片
        skuDesc: "", // 描述
        weight: "", // 重量
        // 收集平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        // 收集图片信息
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        // 收集销售属性列表
        skuSaleAttrValueList: [
          // {
          //   saleAttrId: 0,
          //   saleAttrValueId: 0,
          // },
        ],
      },
    };
  },
  methods: {
    // 获取相关数据
    async getData(row, id) {
      const { category1Id, category2Id, category3Id } = id;

      // 获取图片数据
      let imageRes = await this.$API.sku.reqSpuImageList(row.id);
      if (imageRes.code == 200) {
        this.imageList = imageRes.data;
        this.imageList.forEach((element) => {
          this.$set(element, "isDefault", 0);
        });
      }

      // 获取销售属性列表
      let saleRes = await this.$API.sku.reqSpuSaleAttrList(row.id);
      if (saleRes.code == 200) {
        this.saleList = saleRes.data;
      }

      // 获取平台属性
      let infoRes = await this.$API.sku.reqAttrInfoList(
        category1Id,
        category2Id,
        category3Id
      );
      if (infoRes.code == 200) {
        this.infoList = infoRes.data;
      }

      // 收集需要数据
      this.spuName = row.spuName;
      this.skuInfo.category3Id = category3Id;
      this.skuInfo.spuId = row.id;
      this.skuInfo.tmId = row.tmId;
    },
    // table表格复选框事件
    handleSelectionChange(params) {
      // params是一个数组，保存的是勾选的每一项的信息
      this.skuInfo.skuImageList = params;
    },
    // 设置默认图片
    setDefault(row) {
      this.imageList.forEach((element) => {
        element.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    // 取消按钮(复用spuForm的取消事件)
    cancel() {
      this.$emit("changeScene", {
        scene: "02",
      });
      Object.assign(this._data, this.$options.data());
    },
    // 保存按钮
    async save() {
      // 整理参数
      const { imageList, saleList, infoList, skuInfo } = this;

      skuInfo.skuAttrValueList = infoList.reduce((prev, item) => {
        // 如果存在id信息说明被选中
        if (item.idInfo) {
          // 将id信息分割为数组形式并赋值
          const [attrId, valueId] = item.idInfo.split(":");
          // 将对象push到返回结果的数组中
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);

      skuInfo.skuSaleAttrValueList = saleList.reduce((prev, item) => {
        if (item.idInfo) {
          const [saleAttrId, saleAttrValueId] = item.idInfo.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);

      //发请求
      let res = await this.$API.sku.reqSaveSkuInfo(skuInfo);
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.$emit("changeScene", {
          scene: "02",
        });
      }
    },
  },
};
</script>

<style>
</style>